﻿using CheatMenu.Managers;
using CheatMenu.Utils;
using System.Collections.Generic;
using UnityEngine;

namespace CheatMenu.Menus
{
    public class MenuScreenshot : MenuBase
    {
        private GUIStyle defaultFontControlHeightButtonStyle;
        private GUIStyle defaultFontNoWrapLabelHeaderStyle;
        private GUIStyle normalButtonStyle;
        private GUIStyle labelHeaderStyle;

        private int selectedResolutionIndex = 0;
        private float[] resolutionScales = new float[] { 1f, 2f, 4f };
        private bool canTakeScreenshot = true;
        private int selectedImageDataType = 0;
        private ImageDataType[] imageDataTypes = new ImageDataType[] { ImageDataType.JPG, ImageDataType.PNG };

        public MenuScreenshot() : base("Screenshot", null)
        {
            Alignment = Alignment.Centre;
            ScreenshotUtil.ScreenshotSaved += (r) =>
            {
                canTakeScreenshot = true;
                MenuManager.GetMenu<MenuNotifications>().PushNotification("Saved Screenshot:", r.SavePath);
            };
        }

        protected override Vector2 InitializeSize()
        => new Vector2(GUIManager.ControlWidth + (GUIManager.PaddingSize * 2), GUIManager.ControlHeight + (GUIManager.PaddingSize * 2));

        protected override void InitializeGUI()
        {
            defaultFontControlHeightButtonStyle = new GUIStyle(GUI.skin.button)
            {
                fontSize = GUIManager.ControlFontSize,
                fixedHeight = GUIManager.ControlHeight
            };

            defaultFontNoWrapLabelHeaderStyle = new GUIStyle(GUI.skin.label)
            {
                fontSize = GUIManager.ControlFontSize,
                fontStyle = FontStyle.Bold,
                wordWrap = false,
                normal = new GUIStyleState
                {
                    textColor = GUIManager.LabelHeaderColor
                }
            };

            normalButtonStyle = new GUIStyle(GUI.skin.button)
            {
                margin = new RectOffset(0, 0, 0, 0),
                padding = new RectOffset(0, 0, 0, 0),
                fontSize = GUIManager.ControlFontSize,
                fixedHeight = GUIManager.ControlHeight
            };

            labelHeaderStyle = new GUIStyle(GUI.skin.label)
            {
                fixedHeight = GUIManager.ControlHeight,
                margin = new RectOffset(0, 0, 0, 0),
                padding = new RectOffset(0, 0, 0, 0),
                fontStyle = FontStyle.Bold,
                fontSize = GUIManager.ControlFontSize,
                normal = new GUIStyleState
                {
                    textColor = GUIManager.LabelHeaderColor
                }
            };
        }

        protected override void MenuWindow(int id)
        {
            GUILayout.BeginVertical();
            {
                GUILayout.BeginHorizontal();
                {
                    GUILayout.BeginVertical(GUI.skin.box);
                    {
                        GUILayout.Label("EXPERIMENTAL", defaultFontNoWrapLabelHeaderStyle);
                    }
                    GUILayout.EndVertical();
                    GUILayout.FlexibleSpace();
                    GUILayout.BeginHorizontal(GUI.skin.box);
                    {
                        if (GUILayout.Button("Close", defaultFontControlHeightButtonStyle))
                        {
                            Shown = false;
                        }
                    }
                    GUILayout.EndHorizontal();
                }
                GUILayout.EndHorizontal();

                GUILayout.BeginVertical(GUI.skin.box);
                {
                    GUILayout.Label("Resolution", labelHeaderStyle);
                    GUILayout.BeginVertical(GUI.skin.box);
                    {
                        Color oldBackground = GUI.backgroundColor;
                        for (int i = 0; i < resolutionScales.Length; i++)
                        {
                            if (i == selectedResolutionIndex)
                                GUI.backgroundColor = GUIManager.SelectedButtonColor;
                            else
                                GUI.backgroundColor = oldBackground;

                            float rScale = resolutionScales[i];
                            if (GUILayout.Button(string.Format("{0:0}x ({1:0}x{2:0})", rScale, Screen.width * rScale, Screen.height * rScale), normalButtonStyle))
                                selectedResolutionIndex = i;
                        }
                        GUI.backgroundColor = oldBackground;
                    }
                    GUILayout.EndVertical();
                }
                GUILayout.EndVertical();

                GUILayout.BeginVertical(GUI.skin.box);
                {
                    GUILayout.Label("Compression", labelHeaderStyle);
                    GUILayout.BeginHorizontal(GUI.skin.box);
                    {
                        Color oldBackground = GUI.backgroundColor;
                        for (int i = 0; i < imageDataTypes.Length; i++)
                        {
                            ImageDataType type = imageDataTypes[i];
                            if (i == selectedImageDataType)
                                GUI.backgroundColor = GUIManager.SelectedButtonColor;
                            else
                                GUI.backgroundColor = oldBackground;

                            if (GUILayout.Button(imageDataTypes[i].ToString(), normalButtonStyle))
                                selectedImageDataType = i;
                        }
                        GUI.backgroundColor = oldBackground;
                    }
                    GUILayout.EndHorizontal();
                }
                GUILayout.EndVertical();

                bool guiEnabled = GUI.enabled;
                GUI.enabled = canTakeScreenshot && guiEnabled;
                if (GUILayout.Button("Take Screenshot", normalButtonStyle))
                {
                    canTakeScreenshot = false;
                    bool success = ScreenshotUtil.TakeScreenShot(null, resolutionScales[selectedResolutionIndex], imageDataTypes[selectedImageDataType]);
                    if (!success)
                        MenuManager.GetMenu<MenuNotifications>().PushNotification("Failed to take screenshot!", "Yeah, about that...");
                }
                GUI.enabled = guiEnabled;
            }
            GUILayout.EndVertical();
        }
    }
}
